<script setup lang="ts">
import { getCurrentInstance, ref, reactive, onMounted } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const userInfo = proxy.$Store.get('userInfo') || {}
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const stamp = reactive({
  add: true,
  maxnum: 1,
  forFree: 1,
  setTrueStyle: {},
  imgs: [],
  option: {
    viewMode: 0,
    dragMode: 'move',
    aspectRatio: 1,
    autoCropArea: 1,
    center: false,
    cropBoxMovable: false,
    cropBoxResizable: false,
    toggleDragModeOnDblclick: false
  },
  cropImg: proxy.$Global.cropPngImg
})
// ----
let globalData = {}
const form = reactive({
  voucher: '',
  quality: '',
  company: '',
  msg: '',
  desc: '',
  foot: ''
})

/* ---- */
const uploadoss = ref()
const layer = ref()
const layerLoading = ref()

/* ---- */
const getFormInfo = () => {
  stamp.setTrueStyle = { height: `${document.querySelector('.powerBox dl dd label').clientWidth / stamp.forFree}px` }
  // ----
  proxy.$Myfun.getJsonData(logUser.ads_content, (resJson) => {
    const RJData = resJson[0] || {}
    globalData = RJData
    // ----
    const MData = RJData.material || {}
    if (MData.voucher) form.voucher = MData.voucher
    if (MData.quality) form.quality = MData.quality
    if (MData.company) form.company = MData.company
    if (MData.msg) form.msg = MData.msg
    if (MData.desc) form.desc = MData.desc
    if (MData.foot) form.foot = MData.foot
    // ----
    const forStamp = MData.stamp || []
    forStamp.forEach((item) => {
      stamp.imgs.push({
        imgName: item.name,
        imgUrl: item.image,
        imgW: item.width,
        imgH: item.height
      })
    })
    if (Number(stamp.imgs.length) >= Number(stamp.maxnum) && Number(stamp.maxnum) > 0) stamp.add = !stamp.add
  })
}
// --上传图片操作--
const uploadAct = (type, selectObj, event, index) => {
  const uploadOss = uploadoss.value
  switch (Number(type)) {
    case 1: {
      uploadOss.uploadImg(event, selectObj,'imgs', index)
      break
    }
    case 2: {
      uploadOss.delInfo(selectObj, event)
      break
    }
  }
}
// ----
const emptyInfo = (event) => {
  const curEL = event.currentTarget
  const input = curEL.parentElement.querySelector('input')
  input.value = ''
  input.dispatchEvent(new Event('input'))
}
// ----
const subform = async () => {
  const stampItems = []
  if (Number(stamp.imgs.length) > 0) {
    stamp.imgs.forEach((item) => {
      stampItems.push({
        name: item.imgName,
        image: item.imgUrl,
        width: item.imgW,
        height: item.imgH
      })
    })
  }
  // ----
  globalData.material = {
    voucher: form.voucher,
    quality: form.quality,
    company: form.company,
    msg: form.msg,
    desc: form.desc,
    foot: form.foot,
    stamp: stampItems
  }
  // ----
  const forAdsCon = JSON.stringify(globalData)
  // ----
  const forType = 'patch'
  const forUrl = 'dealer/update?id=' + logUser.id
  const forData = {
    ads_content: forAdsCon,
    // ----
    config_id: userInfo.config_id,
    openid: userInfo.openid
  }
  // ----
  proxy.$Myfun.onLoading(layerLoading.value, 'open', proxy.$Global.loadingMsg)
  let resData = ''
  await proxy.$http[forType](forUrl, forData).then((res) => { resData = res }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
  // ----
  proxy.$Myfun.disLoading(layerLoading.value, 'close')
  if (resData) proxy.$router.back()
}
const goback = () => { proxy.$router.back() }

/* ---- */
onMounted(() => {
  getFormInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <div class="formSet">
      <ul>
        <li>
          <div class="form-txt xw-radius-3 xw-flex xw-flex-align">
            <i>制单人</i>
            <p class="xw-flex-W1"><input class="text" placeholder="" v-model.trim="form.voucher"/></p>
            <transition name="fade"><em class="fa fa-times-circle" @click="emptyInfo" v-if="form.voucher"></em></transition>
          </div>
        </li>
        <li>
          <div class="form-txt xw-radius-3 xw-flex xw-flex-align">
            <i>质检员</i>
            <p class="xw-flex-W1"><input class="text" placeholder="" v-model.trim="form.quality"/></p>
            <transition name="fade"><em class="fa fa-times-circle" @click="emptyInfo" v-if="form.quality"></em></transition>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>企业名称</h1>
            <p><textarea class="tarea-H3" placeholder="" v-model.trim="form.company"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>表头简短说明</h1>
            <p><textarea class="tarea-H3" placeholder="" v-model.trim="form.msg"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>表头简短广告词</h1>
            <p><textarea class="tarea-H3" placeholder="" v-model.trim="form.desc"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-are xw-radius-3">
            <h1>表尾说明</h1>
            <p><textarea class="tarea-H3" placeholder="" v-model.trim="form.foot"></textarea></p>
          </div>
        </li>
        <li>
          <div class="form-imgs xw-radius-3 powerBox">
            <div class="FIS-top xw-flex xw-flex-align xw-flex-justify">
              <span class="FIS-title">材质书质检图章</span>
            </div>
            <dl class="FIS-img">
              <dd v-for="(item, index) in stamp.imgs"><label class="xw-block xw-act"><img :src="item.imgUrl"/><input type="file" style="display: none;" accept="image/*" @change="uploadAct(1, stamp, $event, index)"/></label><span class="FIS-del fal fa-times xw-act" @click="uploadAct(2, stamp, index)"></span></dd>
              <dd v-if="stamp.add"><label class="xw-act xw-flex xw-flex-center" :style="stamp.setTrueStyle"><em class="fal fa-plus"></em><input type="file" style="display: none;" accept="image/*" @change="uploadAct(1, stamp, $event)"/></label></dd>
            </dl>
          </div>
        </li>
      </ul>
      <div class="form-sub xw-radius-3 xw-bgCL-11 xw-act" @click="subform">确定提交</div>
      <div class="form-sub xw-radius-3 xw-WhiteRed xw-act" @click="goback">取消返回</div>
    </div>
    <uploadoss-view ref="uploadoss"/>
    <layer-view ref="layer"/>
    <layer-view ref="layerLoading"/>
  </div>
</template>